#include<iostream>
#include<string>
#include<fstream>
#include"lexer_c.hpp"
using St=std::string;
void copyFile(std::string&dst,std::string const&fileName){
    std::ifstream ifs(fileName,std::ios::in);
    char ch;
    if(ifs.is_open()){
        ifs>>std::noskipws;
        while(!ifs.eof()){
            ifs>>ch;
            if(ifs.eof())break;
            dst+=ch;
        }ifs.close();
    }
}
void readFile(std::string&dst,std::string const&fileName){// errors
    std::ifstream ifs(fileName,std::ios::in);
    char ch;
    std::string temp;
    if(ifs.is_open()){
        ifs>>std::noskipws;
        while(!ifs.eof()){
            ifs>>temp;
            if(ifs.eof())break;
            dst+=temp;
        }ifs.close();
    }
}
size_t numCheck(size_t p,std::string const&str,std::string&dst){// [0-9]+(\.[0-9]+)?
        char next;size_t st=p;
        while(p!=str.size()&&(std::isdigit(next=str[++p])||next=='.')){
            if(next=='.'){
                if(p!=str.size()&&std::isdigit(str[p+1])){
                    dst+='.';
                }
                else return st;// error
            }
            else dst+=next;
        }return p;
    }
size_t get(size_t p,std::string const&str,std::string&dst){// [_A-Za-z][_A-Za-z0-9]*
        size_t ptr=p;char next=str[++ptr];
        std::cout<<"ptr:"<<p<<'\t';
        while(next&&(std::isalpha(next)||std::isdigit(next)||'_'==next)){// identifier
            dst+=next;
            next=str[++ptr];
        }
        return ptr;
    }
void lexer(){
    std::list<std::string>files;
    // files.push_back("normal.cpp");
    files.push_back("err.cpp");
    std::string file;
    int i=0;
    for(auto&&it:files){
        // std::cout<<"Part : "<<i<<std::endl;
        // copyFile(file,it);
        // std::cout<<file<<std::endl;
        // lexer_c(file,it).execute().show();
        lexer_c(it).execute().show();
        file.clear();
        ++i;
    }
}
void lt1(){
    St s1("abc 3"),s2;
    std::cout<<get(0,s1,s2)<<"||"<<s2;
}
void lt2(){
    St s1("0.36"),s2;
    std::cout<<numCheck(0,s1,s2)<<"||"<<s2;
}
void rf(){
    St file("err.cpp"),dst;
    readFile(dst,file);
    std::cout<<dst;
}
int main(){
    // lt2();
    lexer();
    // rf();
    return 0;
}